package picol.view;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.LayoutManager2;
import java.awt.Toolkit;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author Aurélien Marcel - Julien Sanderson
 */
public abstract class ViewAbs extends JFrame implements View {

    protected JPanel mainPanel;
    protected int frame_width;
    protected int frame_height;
    protected LayoutManager2 mainLayout;

    public ViewAbs() {
        super();
        this.mainLayout = new GridBagLayout();
        this.init();
        this.pack();
        this.setSize(frame_width, frame_height);
        this.center(frame_width, frame_height);
        this.setVisible(true);
    }

    public ViewAbs(String title) {
        super(title);
        this.mainLayout = new GridBagLayout();
        this.init();
        this.pack();
        this.setSize(frame_width, frame_height);
        this.center(frame_width, frame_height);
        this.setVisible(true);
    }

    public void display() {
        this.setVisible(true);
    }

    public void close() {
        this.setVisible(false);
    }

    private void init() {
        this.mainPanel = new JPanel(this.mainLayout);
        this.add(this.mainPanel);

        this._init();
        this.listening();
    }

    protected abstract void _init();

    private void center(int _width, int _height) {

        Dimension tailleEcran = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (tailleEcran.width - _width) / 2;
        int y = (tailleEcran.height - _height) / 2;
        this.setBounds(x, y, _width, _height);
    }

    /**
     * Add component on the main panel
     * @param _component
     * @param _x
     * @param _y
     */
    protected void addComponent(JComponent _component, int _x, int _y) {
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = _x;
        constraints.gridy = _y;
        this.mainPanel.add(_component, constraints);
    }

    /**
     * Add component on a panel
     * @param _component
     * @param _x
     * @param _y
     * @param _panel
     */
    protected void addComponent(JComponent _component, int _x, int _y, JPanel _panel) {
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = _x;
        constraints.gridy = _y;
        _panel.add(_component, constraints);
    }
}
